package com.shaozi.nc.quartz.attendance;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.data.hibVO.AttendanceLevelVO;
import com.shaozi.nc.hibservice.AttendanceLevelService;
import com.shaozi.nc.manage.secretary.AttendanceManage;
import com.shaozi.nc.quartz.base.BaseRuleJobExecute;
import com.shaozi.nc.quartz.manage.AttendanceSignRuleManage;

/**
 * 考勤签退，签到提醒，本类，用于读取考勤规则设置动态的提醒时间。
 * <p>
 * 1.读取每日考勤数据是否生成成功，不成功则通知到特定IM.并退出<br>
 * 2.根据规则时间设置一次性调度计划。
 * 
 * @author xueshuwu
 *
 */
public class AttendanceSignRuleJobExecute extends BaseRuleJobExecute {

	private Logger logger = Logger.getLogger(AttendanceSignRuleJobExecute.class);
	@Autowired
	private AttendanceManage attendanceManage;

	@Autowired
	private AttendanceLevelService attendanceLevelService;

	@Autowired
	private AttendanceSignRuleManage attendanceSignRuleManage;

	private boolean ignoreDataInit = false;

	public AttendanceSignRuleJobExecute() {
		super();
	}

	@Override
	protected void handleCompany(String companyId) {
		try {
			if (!ignoreDataInit) {
				boolean ok = attendanceManage.isTodaySuccInit(companyId);
				if (!ok) {
					// TODO 通知到IM
					return;
				}
			}
			scheduleByAttendanceLevel(companyId);
		} catch (Exception e) {
			// TODO 异常通知到IM
			logger.error(SzException.getStackMessage(e));
		}
	}

	private void scheduleByAttendanceLevel(String companyId) throws SchedulerException, SzException,
			IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		List<AttendanceLevelVO> levelVOs = attendanceLevelService.loadList(companyId);
		attendanceSignRuleManage.scheduleByAttendanceLevel(levelVOs, companyId);
	}

	public boolean isIgnoreDataInit() {
		return ignoreDataInit;
	}

	public void setIgnoreDataInit(boolean ignoreDataInit) {
		this.ignoreDataInit = ignoreDataInit;
	}

}
